package com.tastyshop.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tastyshop.dao.RecommendMapper;
import com.tastyshop.domain.PageHelper;
import com.tastyshop.domain.RecommendBoard;
import com.tastyshop.domain.Recommend_Reply;

@Service
@Transactional
public class RecommendServiceImpl implements RecommendService{
	
	@Autowired
	private RecommendMapper mapper;
	
	private static final int LINES_PER_PAGE = 10;

	@Override
	@Transactional(readOnly = true)
	public List<RecommendBoard> getAllBoardByPage(Integer page) {
		return mapper.getAllBoardByPage(new PageHelper(LINES_PER_PAGE, page.intValue()));
	}
	
	@Override
	@Transactional(readOnly = true)
	public RecommendBoard getBoardByIdx(Integer idx) {
		return mapper.getBoardByIdx(idx);
	}
	
	@Override
	@Transactional(readOnly = true)
	public Integer getLastPage() {
		Integer total = mapper.getBoardTotal();
		return new Integer((int)Math.ceil((double)total/LINES_PER_PAGE));
	}

	@Override
	public void addBoard(RecommendBoard recommendBoard) {
		mapper.addBoard(recommendBoard);
	}

	@Override
	public void modifyBoard(RecommendBoard recommendBoard) {
		mapper.modifyBoard(recommendBoard);		
	}

	@Override
	public void raiseLookupCount(Integer idx) {
		mapper.raiseLookup(idx);		
	}

	@Override
	@Transactional(readOnly = true)
	public List<Recommend_Reply> getAllRepliesByIdx(Integer idx) {
		return mapper.getAllRepliesByIdx(idx);
	}

	@Override
	public HashMap<String, RecommendBoard> getInfo(Integer idx) {
		HashMap<String, RecommendBoard> map = new HashMap<>();
		map.put("recommendBoard", mapper.getInfo(idx));
		map.put("prev", mapper.getPrev(idx));
		map.put("next", mapper.getNext(idx));
		return map;
	}

	@Override
	public void addReply(Recommend_Reply recommend_Reply) {
		mapper.addReply(recommend_Reply);
		mapper.raiseReplyCount(recommend_Reply.getRef_no());
	}

	@Override
	public void modifyReply(Recommend_Reply recommend_Reply) {
		mapper.modifyReply(recommend_Reply);
		
	}

	@Override
	public void removeReply(Recommend_Reply recommend_Reply) {
		mapper.removeReply(recommend_Reply.getIdx());
		mapper.lowerReplyCount(recommend_Reply.getRef_no());
		System.out.println(recommend_Reply.getRef_no());
	}

	@Override
	public void removeBoard(Integer idx) {
		mapper.removeBoard(idx);
		mapper.removeAllReplies(idx);
	}
}
